home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Source / MiscKit / MiscStringArray.m < prev    next >
Encoding:
Text File  |  1995-07-06  |  6.0 KB  |  267 lines

  1. //
  2. //    MiscStringArray.m -- a generic class to store lists of strings
  3. //        Originally written by Drew Davidson
  4. //        Copyright (c) 1994 by Drew Davidson.
  5. //        Modified and extended by Don Yacktman for inclusion into the MiscKit.
  6. //                Version 1.4  All rights reserved.
  7. //        This notice may not be removed from this source code.
  8. //
  9. //    This object is included in the MiscKit by permission from the author
  10. //    and its use is governed by the MiscKit license, found in the file
  11. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  12. //    for a list of all applicable permissions and restrictions.
  13. //    
  14.  
  15. #define MISCSTRINGARRAY_VERSION 2
  16.  
  17. /*----------------------------------------------------------------------------
  18.     $Source$
  19.  
  20.     SYNOPSIS
  21.         Implements an array of char * values built from MiscString objects.
  22.  
  23.     REVISIONS
  24.     $Log$
  25. ----------------------------------------------------------------------------*/
  26. # import <misckit/misckit.h>
  27.  
  28. @interface MiscStringArray(private)
  29.  
  30. - _invalidateArray;
  31. - _buildArray;
  32.  
  33. @end
  34.  
  35. @implementation MiscStringArray
  36.  
  37. /*----------------------------< PRIVATE METHODS >----------------------------*/
  38. - _invalidateArray
  39. {
  40.     NX_FREE(stringArray);
  41.     stringArray = NULL;
  42.     return(self);
  43. }
  44.  
  45. - _buildArray
  46. {
  47.     if (stringArray)
  48.         [self _invalidateArray];
  49.     NX_MALLOC(stringArray,const char *,[strings count]+1);
  50.     MISCforAllListObjects(strings,string)
  51.     {    stringArray[MISCCurrentSlot] = ((uniqued) ? [string uniqueStringValue] : [string stringValue]);
  52.         // stringArray[MISCCurrentSlot+1] = NULL; // Carl says unneeded
  53.     } MISCendFor
  54.     stringArray[[self count]] = NULL; // Carl added
  55.     return(self);
  56. }
  57.  
  58. /*---------------------------< INIT/FREE METHODS >---------------------------*/
  59. + initialize
  60. {
  61.     if ( self == [MiscStringArray class] )
  62.         [MiscStringArray setVersion:MISCSTRINGARRAY_VERSION];
  63.  
  64.     return self;
  65. }
  66.  
  67. - init
  68. {
  69.     [super init];
  70.     strings = [[List allocFromZone:[self zone]] initCount:0];
  71.     uniqued = NO;
  72.     stringArray = NULL;
  73.     return(self);
  74. }
  75.  
  76. - free
  77. {
  78.     [[strings freeObjects] free];
  79.     [self _invalidateArray];
  80.     return([super free]);
  81. }
  82.  
  83. /*--------------------------< OVERRIDDEN METHODS >---------------------------*/
  84. - copyFromZone:(NXZone *)aZone
  85. {
  86.     self = [super copyFromZone:aZone];
  87.     strings = [strings deepCopyFromZone:aZone];
  88.     stringArray = NULL;
  89.     [self _buildArray];
  90.     return(self);
  91. }
  92.  
  93. /*-----------------------------< OTHER METHODS >-----------------------------*/
  94. - (BOOL)uniqued
  95. {
  96.     return(uniqued);
  97. }
  98.  
  99. - setUniqued:(BOOL)yn
  100. {
  101.     if (yn != uniqued)
  102.     {    uniqued = yn;
  103.         [self _invalidateArray];
  104.     }
  105.     return(self);
  106. }
  107.  
  108. - strings
  109. {
  110.     [self _invalidateArray];
  111.     return(strings);
  112. }
  113.  
  114. - addStringObject:(MiscString *) aStringObj // -bbum
  115. {
  116.     [self _invalidateArray];
  117.     [[self strings] addObject:aStringObj];
  118.     return(self);
  119. }
  120.  
  121. - addString:(const char *)aString
  122. {
  123.     [self _invalidateArray];
  124.     [[self strings] addObject:[[MiscString allocFromZone:[self zone]] initString:aString]];
  125.     return(self);
  126. }
  127.  
  128. - removeString:(const char *)aString
  129. {    // This method will remove aString from the MiscStringArray. If the string
  130.     // doesn't exist, it will  return nil, otherwise it will return self.
  131.     // contributed by Juergen Zeller <zet@cip.e-technik.uni-erlangen.de>
  132.     unsigned int index = MISC_NOT_IN_ARRAY;
  133.  
  134.     index = [self indexOfString:aString];
  135.     if (index == MISC_NOT_IN_ARRAY) return (nil);
  136.     [self _invalidateArray];
  137.     [[[self strings] removeObjectAt:index] free];
  138.     return (self);
  139. }
  140.  
  141.  
  142. - insertString:(const char *)aString at:(unsigned int)index
  143. {
  144.     [self _invalidateArray];
  145.     [[self strings] insertObject:[[MiscString allocFromZone:[self zone]] initString:aString] at:index];
  146.     return(self);
  147. }
  148.  
  149. - (unsigned int)stringCount
  150. {
  151.     return([strings count]);
  152. }
  153.  
  154. - (unsigned int)count
  155. {
  156.     return([strings count]);
  157. }
  158.  
  159. - empty // -bbum
  160. {
  161.     // invalidate stringArray
  162.     [self _invalidateArray];
  163.  
  164.     // empty and free all strings contained in strings List
  165.     [[strings freeObjects] empty];
  166.  
  167.     return self;
  168. }
  169. - sort { return [strings miscStringSort]; } // bbum
  170.  
  171. - (const char **)stringArray
  172. {
  173.     if (!stringArray)
  174.         [self _buildArray];
  175.     return(stringArray);
  176. }
  177.  
  178. - (const char *)stringAt:(unsigned int)index
  179. {    const char        **array = [self stringArray];
  180.  
  181.     return(array[index]);
  182. }
  183.  
  184. - (unsigned int)indexOfString:(const char *)aString
  185. {    unsigned int    index = MISC_NOT_IN_ARRAY;
  186.     
  187.     MISCforAllListObjects(strings,string)
  188.     {    if ([string cmp:aString] == 0)
  189.         {    index = MISCCurrentSlot;
  190.             break;
  191.         }
  192.     } MISCendFor
  193.     return(index);
  194. }
  195.  
  196. - (int)browser:sender fillMatrix:matrix inColumn:(int)column
  197. {
  198.     int i;
  199.     id cellList, theCell;
  200.  
  201.     // Set matrix to have the right number of cells.
  202.     [matrix renewRows:[self count] cols:1];
  203.  
  204.     // Get list of cells from the matrix.
  205.     cellList = [matrix cellList];
  206.  
  207.     // For each cell set its value, set whether it is a leaf or not and
  208.     //   mark it loaded.
  209.     for(i=0; i<[cellList count]; i++) {
  210.         theCell = [cellList objectAt:i];
  211.         [theCell setStringValue:[self stringAt:i]];
  212.         [theCell setLeaf:YES];
  213.         [theCell setLoaded:YES];
  214.     }
  215.  
  216.     // Return the number of rows.
  217.     return [self count];
  218. }
  219.  
  220. /*
  221.  * Basic read/write methods.
  222.  * Note that we aren't writing the const char **stringArray at the moment,
  223.  * because (a) I'm not sure how to handle that, and (b) I think we can
  224.  * get away without it.
  225.  * shayman Aug 12 1994
  226.  */
  227. - read:(NXTypedStream *)stream
  228. {
  229.     int version;
  230.  
  231.     [super read:stream];
  232.     version = NXTypedStreamClassVersion(stream, [MiscStringArray name]);
  233.     
  234.     switch (version) {
  235.         case MISCSTRINGARRAY_VERSION: {
  236.             NXReadTypes(stream, "@c", &strings, &uniqued);
  237.             break;
  238.         }
  239.         case 0: { int un;
  240.         // we were writing a BOOL as an int--that's dangerous, so, rather
  241.         // than bump the version, I changed it to convert between int/BOOL
  242.         // on the write and the read.
  243.             NXReadTypes(stream, "@i", &strings, &un);
  244.             uniqued = (un ? YES : NO);
  245.             break;
  246.         }
  247.         default: {
  248.             NXLogError("[%s %s] - unknown version of %s in typed stream",
  249.                     [[self class] name], sel_getName(_cmd), 
  250.                     [[self class] name]);
  251.             break;
  252.         }
  253.     }
  254.     return self;
  255. }
  256.  
  257. - write:(NXTypedStream *)stream
  258. {
  259.     [super write:stream];
  260.     // we were writing a BOOL as an int--that's dangerous, so now we
  261.     // use a "c" for it.  This is safe.
  262.     NXWriteTypes(stream, "@c", &strings, &uniqued);    
  263.     return self;
  264. }
  265.  
  266. @end
  267.